package com.cn.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.cn.dao.UserDao;
import com.cn.domain.User;
import com.cn.utils.JdbcUtils;

public class UserDaoImpl2 implements UserDao {

	private QueryRunner qr = new QueryRunner();
	
	public User getUserByUserName(User u) {
		Connection conn = JdbcUtils.getConnection();
		String sql = "select * from user where username = ?";
		try {
			User user = qr.query(conn, sql, new BeanHandler<User>(User.class),u.getUsername());
			//System.out.println(user.getPassword());//上一步从数据库中取出密码后，又再进行加密，所以此时输出的密码和数据库中不一样，是数据库中的密码又经进一步的加密
			return user;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("根据用户名查询用户失败！");
		}
		
	}

	public void save(User u) {
		Connection conn = JdbcUtils.getConnection();
		String sql = "INSERT INTO USER " + 
					" (id,       "+
					" username,  "+
					" PASSWORD,  "+
					" gender,    "+
					" email,     "+
					" telephone, "+
					" introduce, "+
					" registTime, "+
					" activecode, "+
					" isactive "+
					" )          "+
					"	VALUES   "+
						"(null,?,?,?,?,?,?,?,?,?);";
		try {
			qr.update(conn, sql, u.getUsername(),u.getPassword(),u.getGender(),
					u.getEmail(),u.getTelephone(),u.getIntroduce(),u.getRegistTime(),u.getActivecode(),u.getIsactive());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("保存用户失败！");
		}

	}

	public User getUserByUserName(String name) {
		Connection conn = JdbcUtils.getConnection();
		String sql = "select * from user where username = ?";
		try {
			User user = qr.query(conn, sql, new BeanHandler<User>(User.class),name);
			return user;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("根据用户名查询用户失败！");
		}
	}

	public User getUserByActiveCode(String activecode) {
		Connection conn = JdbcUtils.getConnection();
		String sql = "select * from user where activecode = ?";
		try {
			User user = qr.query(conn, sql, new BeanHandler<User>(User.class),activecode);
			return user;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("根据激活码查询用户失败！");
		}
	}

	public void updateActive(int id, int isactive) {
		Connection conn = JdbcUtils.getConnection();
		String sql = "update user set isactive = ? where id = ?";
		try {
			qr.update(conn, sql, isactive,id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("根据激活码状态失败！");
		}
	}

//	public User getUserByUserId(int id) {
//		Connection conn = JdbcUtils.getConnection();
//		String sql = "select * from user where id = ?";
//		try {
//			User user = qr.query(conn, sql, new BeanHandler<User>(User.class),id);
//			return user;
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			throw new RuntimeException("根据用户id查询用户失败！");
//		}
//	}

	public void updateUser(User user) {
		Connection conn = JdbcUtils.getConnection();
		String sql = "update user set password = ?,gender = ?,telephone=? where id = ?";
		try {
			qr.update(conn, sql, user.getPassword(),user.getGender(),user.getTelephone(),user.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("更新用户信息失败！");
		}
		
	}

}
